# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pre-commit
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=4.2.0
pkgrel=1
pkgdesc="A framework for managing and maintaining multi-language pre-commit hooks. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: pre-commit'
  'purl: pkg:pypi/pre-commit'
)
url='https://pre-commit.com/'
msys2_repository_url='https://github.com/pre-commit/pre-commit'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-cfgv"
         "${MINGW_PACKAGE_PREFIX}-python-identify"
         "${MINGW_PACKAGE_PREFIX}-python-nodeenv"
         "${MINGW_PACKAGE_PREFIX}-python-yaml"
         "${MINGW_PACKAGE_PREFIX}-python-virtualenv")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname/-/_}-${pkgver}.tar.gz"
        '001-use-linux-virtualenv-dirs.patch')
sha256sums=('601283b9757afd87d40c4c4a9b2b5de9637a8ea02eaff7adc2d0fb4e04841146'
            '9d91c936d7b7449ebbcd3ce841810ae6875f82d2d0366024368d923968c36304')

# Helper macro to apply patches
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname/-/_}-${pkgver}"
  apply_patch_with_msg \
    001-use-linux-virtualenv-dirs.patch
}

build() {
  cp -r "${_realname/-/_}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/python -m pytest || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
